function putbid(auction_id, user_key, event) {
try {
	req = new XMLHttpRequest();
}catch(e) {
	try {
		req = new ActiveXObject('Msxml2.XMLHTTP');
	}catch(e) {
		try {
			req = new ActiveXObject('Microsft.XMLHTTP');
		}catch(e) {
			req = false;
		}
	}
}
req.open("GET", "putbid.php?id="+ auction_id +"&key=" + user_key + '&' + Math.random(), true);
req.setRequestHeader('Content-Type', 'application/xml');
req.onreadystatechange = function() {
		if(req.readyState == 4) {
			if(req.status == 200) {
				response = req.responseXML.documentElement;
				if(response != null) {
					allowbid = response.getElementsByTagName('allowbid')[0].firstChild.data;
					nocredits = response.getElementsByTagName('nocredits')[0].firstChild.data;
					if(allowbid == '1') {
						if(nocredits == 'true') {
							showHideAlert('Nu aveti credite.');
						} else {
							window.location = 'main.php?page=24&id='+auction_id;
						}
					}
					if(allowbid == '2') {
						showHideAlert('Licitatia nu a inceput!');
					}
					if(allowbid == '3') {
						showHideAlert('Aveti deja oferta maxima!');
					}
					if(allowbid == '4') {
						showHideAlert('Licitatiile sunt inchise in afara programului!');
					}
				} else {
					showHideAlert('O problema a impiedicat terminarea operatiunii. Creditele dvs. NU au fost consumate!');
				}
			} else {
				showHideAlert('O problema a impiedicat terminarea operatiunii. Creditele dvs. NU au fost consumate!');
			}
		}
	};
req.send(null);
}

// id, mins, sec, 'single' - flag for displaying last 10 offers]
var auction_id_array = new Array();

function countDown(timer_id) {
	if(timer_id != null) {
		if (document.getElementById){ 
			timer = document.getElementById('lblTimeCounter_'+auction_id_array[timer_id][0]);
		}

		if (auction_id_array[timer_id][1] <= 0 && auction_id_array[timer_id][2] <= 0) {
			auction_id_array[timer_id][1] = 0;
			auction_id_array[timer_id][2] = 0;
			auction_id_array[timer_id][3] = window.setTimeout("countDown(" + timer_id + ");", 1000);
			return;
		}

		auction_id_array[timer_id][2]--;
		if (auction_id_array[timer_id][2] == -01) {
			auction_id_array[timer_id][2] = 59;
			auction_id_array[timer_id][1] --; 
		}else{
			auction_id_array[timer_id][1] = auction_id_array[timer_id][1]; 
		}
		
		if (auction_id_array[timer_id][2] <= 9){
			auction_id_array[timer_id][2] = "0" + auction_id_array[timer_id][2]; 
		}

		time = (auction_id_array[timer_id][1]<=9 ? "0" + auction_id_array[timer_id][1] : auction_id_array[timer_id][1]) + ":" + auction_id_array[timer_id][2];
		
		if(auction_id_array[timer_id][1] == 0 && auction_id_array[timer_id][2] == 0) time = "--:--";
		if(time == 'undefined:NaN') time = '';

		if(timer){ 
			timer.className = 'turquoise timer';
			if (auction_id_array[timer_id][1] == '00') {
				if (auction_id_array[timer_id][2] < 30)
					timer.className = 'orange timer';
				if (auction_id_array[timer_id][2] < 10)
					timer.className = 'bold red timer';
			}
			timer.innerHTML = time;
		}
		auction_id_array[timer_id][3] = window.setTimeout("countDown(" + timer_id + ");", 1000);
	} else {
		for (var i=0; i < auction_id_array.length; i++) {
			auction_id_array[i][3] = window.setTimeout("countDown(" + i + ");", 1000);
		}
	}
}

function makeTicker(t_sec, auction_id) {
	sec = t_sec % 60;
	min = (t_sec - sec) / 60;
	if(auction_id) {
		for(i=0; i < auction_id_array.length; i++) {
			if(auction_id_array[i][0] == auction_id) {
				auction_id_array[i][1] = min;
				auction_id_array[i][2] = sec;
				break;
			}
		}
	}
	return ((min < 9) ? "0" : "") + min + ":" + ((sec < 9) ? "0" : "") + sec;
}

function checkTime(i) {
	if (i<10) {
	  i="0" + i;
	}
	return i;
}

function makeLastTenTable(lastTenXml) {
	var lastPerson = lastTenXml[0].getElementsByTagName('onePerson');

	var lastUserName;
	var lastTime;
	var lastPrice;

	var lastTenTable = '';
	if (lastPerson.length > 0) {
		for (var i = 0; i < lastPerson.length; i++) {
			lastUserName = lastPerson[i].getElementsByTagName('username')[0].childNodes[0].nodeValue;
			lastTime     = lastPerson[i].getElementsByTagName('bidTime')[0].childNodes[0].nodeValue;
			lastPrice    = lastPerson[i].getElementsByTagName('bidPrice')[0].childNodes[0].nodeValue;
			lastTenTable += '<br />' + lastUserName + ' - ' + lastTime + ' - ' + lastPrice;
		}

		var participants = document.getElementById('lblOfferList');
		participants.innerHTML = lastTenTable;
	}
}

var XMLReq = false;

function refreshItems(type, id) {
	if(!XMLReq) {
		try {
			XMLReq = new XMLHttpRequest();
		}catch(e) {
			try {
				XMLReq = new ActiveXObject('Msxml2.XMLHTTP');
			}catch(e) {
				try {
					XMLReq = new ActiveXObject('Microsft.XMLHTTP');
				}catch(e) {
					XMLReq = false;
				}
			}
		}
	}
	if(XMLReq) {
		var makeUrl = 'req_xml_data.php';
		if (id && type) {
			makeUrl += '?id=' + id + '&type=' + type + '&' + Math.random();
		} else {
			makeUrl += '?' + Math.random();
		}
		XMLReq.open("GET", makeUrl, true);
		XMLReq.setRequestHeader('Content-Type', 'application/xml');
		XMLReq.onreadystatechange=function() {
		
			if(XMLReq.readyState == 4 && XMLReq.status == 200) {
				var response = null;
				var sleep_status = '';
				try {
					response = XMLReq.responseXML.documentElement;
				} catch (e) {
					//alert(e);
					var refreshTimeout  = setTimeout("refreshItems('" + (type ? type : '') + "', '" + (id ? id : '') + "')", 2000);
					return;
				}
				try {
					sleep_status = response.getElementsByTagName('status')[0].childNodes[0].nodeValue;
				} catch (e) {
					//alert(e);
					var refreshTimeout  = setTimeout("refreshItems('" + (type ? type : '') + "', '" + (id ? id : '') + "')", 2000);
					return;
				}
				//if (sleep_status == 'false') {
					//alert(makeUrl + '\n' + XMLReq.responseText);
					var auctions = XMLReq.responseXML.getElementsByTagName('bidItem');
					var reqArray = new Array();
					if (auctions.length > 0) {
						var auction_id = '';
						var time_to_go = '';
						var price = '';
						var last_user = '';
						var is_open = '';
						var t_date = '';

						var BESTBIDDERSTATE = '';
						var TIMECOUNTER = '';
						var SOLDOROFFER = '';
						var AUCTIONT_DATE= '';
						var TIMERMESSAGE = '';

						for (var i = 0; i < auctions.length; i++) {
							try {
								last_user  = auctions[i].getElementsByTagName('lastBid')[0].childNodes[0].nodeValue;
							} catch(e) {
								last_user = '';
							}
							auction_id = auctions[i].getElementsByTagName('bidId')[0].childNodes[0].nodeValue;
							time_to_go = auctions[i].getElementsByTagName('timeToGo')[0].childNodes[0].nodeValue;
							price      = auctions[i].getElementsByTagName('currentPrice')[0].childNodes[0].nodeValue;
							is_open    = auctions[i].getElementsByTagName('isOpen')[0].childNodes[0].nodeValue;
							t_date     = auctions[i].getElementsByTagName('tDate')[0].childNodes[0].nodeValue;
							
							js_today = eval(response.getElementsByTagName('today')[0].childNodes[0].nodeValue);
							js_t_date = eval(t_date);
							

							if(is_open == 'true') {
								BESTBIDDERSTATE = "Ultimul ofertant";
								TIMECOUNTER = makeTicker(time_to_go);
								if(js_t_date.valueOf() > js_today.valueOf()) {
									if(js_t_date.getDate() == js_today.getDate() && 
										js_t_date.getMonth() == js_today.getMonth() &&
										js_t_date.getUTCFullYear() == js_today.getUTCFullYear()) {
										AUCTIONT_DATE = checkTime(js_t_date.getHours()) + ':' + checkTime(js_t_date.getMinutes());
									} else {
										AUCTIONT_DATE = checkTime(js_t_date.getDate()) + '/' + checkTime(js_t_date.getMonth()) 
											+ '/' + checkTime(js_t_date.getUTCFullYear()) + ' ' + checkTime(js_t_date.getHours()) 
											+ ':' + checkTime(js_t_date.getMinutes());
									}
								} else {
									TIMECOUNTER = makeTicker(time_to_go, auction_id);
									TIMERMESSAGE = 'Timp ramas:';
								}
							} else {
								BESTBIDDERSTATE = 'Cumparator';
								AUCTIONT_DATE = document.getElementById('lblTime_' + auction_id).innerHTML;
							}

							document.getElementById('lblPrice_' + auction_id).innerHTML = price;
							document.getElementById('lblTimeCounter_' + auction_id).innerHTML = TIMECOUNTER;
							document.getElementById('lblBestBidderState_' + auction_id).innerHTML = BESTBIDDERSTATE;
							document.getElementById('lblBestBidder_' + auction_id).innerHTML = last_user;
							document.getElementById('lblTime_' + auction_id).innerHTML = AUCTIONT_DATE;

							if (type == 2) {
								document.getElementById('lblTimer_' + auction_id).innerHTML = TIMERMESSAGE;
								var getLastTen = '';
								getLastTen = auctions[i].getElementsByTagName('lastTen');
								makeLastTenTable(getLastTen);
							}
							
							reqArray.push(auction_id);
						}
					}
					var refreshTimeout  = setTimeout("refreshItems('" + (type ? type : '') + "', '" + (id ? id : '') + "')", 3000);

					for (var i = 0; i < auction_id_array.length; i++) {
						if(!reqArray.contains(auction_id_array[i][0])) {
							auction_id = auction_id_array[i][0];
							auction_id_array[i][1] = 0; 
							auction_id_array[i][2] = 0;
							
							BESTBIDDERSTATE = "Cumparator";
							SOLDOROFFER = "<div class=\"orange-button\">VANDUT</div>";
							AUCTIONT_DATE = 'Incheiata';
							if(document.getElementById('lblBestBidder_' + auction_id).innerHTML == '')
								AUCTIONT_DATE = 'Anulata';
							clearTimeout(auction_id_array[i][3]);
							document.getElementById('auctionbox_'+auction_id).className="box_red";
							document.getElementById('lblBestBidderState_' + auction_id).innerHTML = BESTBIDDERSTATE;
							document.getElementById('lblTimeCounter_' + auction_id).innerHTML = '';
							document.getElementById('lblTime_' + auction_id).innerHTML = AUCTIONT_DATE;
							if(SOLDOROFFER)
								document.getElementById('auction_button_' + auction_id).innerHTML = SOLDOROFFER;
						}
					}
				//}
			}
		};
		XMLReq.send(null);
	}
}
